#include <algorithm>
#include <cstring>
#include "../utility/assert_helper.h"
#include "../utility/file_helper.h"
#include "../win_manager/windows_information.h"
#include "spyware_scanner.h"

Spyware_Scanner::Spyware_Scanner(
const Windows_Information & win_info,
const std::string & specificity_file) :
spec_file_path_(specificity_file), win_info_(win_info), item_num_(0)
{
  std::ifstream infile(spec_file_path_.c_str(), std::ios::binary);
  if(infile) {
		std::istreambuf_iterator <char> begin(infile),end;
		item_num_ = std::count(begin, end, '\n');
		infile.close();
	}
}

Spyware_Scanner::~Spyware_Scanner()
{
}

bool
Spyware_Scanner::find_first(Spyware_Sign & sign)
{
	spec_file_.close();
	spec_file_.open(spec_file_path_.c_str());

	return find_next(sign);
}

bool
Spyware_Scanner::find_next(Spyware_Sign & sign)
{
	if (spec_file_) {
		char	line[1024] = {0};
		spec_file_.getline(line, sizeof(line) - 1);
		if (0 < strlen(line)) {
			std::string str(line);
			strncpy(line, str.substr(str.find_first_of(':') + 1).c_str(), sizeof(line) - 1);
			line[sizeof(line) - 1] = '\0';
			win_info_.path_expand(line, 1023);

			if (utility::is_file_exist(line)) {

				sign.first = str.substr(0, str.find_first_of(':'));
				sign.second = HARM_MIDDLE;
			} else {
				sign.first = "";
				sign.second = HARM_UNKNOW;
			}
		}
		return true;
	}
	return false;
}

int
Spyware_Scanner::kill_item(const Spyware_Sign & sign) const
{
	return -1;
}

int
Spyware_Scanner::get_item_total() const
{
	return item_num_;
}
